from django.db import transaction
from rest_framework import status
from rest_framework.response import Response


class Result:
    def __init__(self, code, message, data):
        self.code = code
        self.message = message
        self.data = data

    @staticmethod
    def success(message, data):
        return Response({
            'code': 200,
            'message': message,
            'data': data
        }, status=status.HTTP_200_OK)

    @staticmethod
    def error(message, error=None):
        transaction.set_rollback(True)
        return Response({
            'code': 500,
            'message': message,
            'error': error
        }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    @staticmethod
    def refuse(message):
        transaction.set_rollback(True)
        return Response({
            'code': 403,
            'message': message,
        }, status=status.HTTP_403_FORBIDDEN)

    @staticmethod
    def illegal(message, error=None):
        transaction.set_rollback(True)
        return Response({
            'code': 400,
            'message': message,
            'error': error
        }, status=status.HTTP_400_BAD_REQUEST)
